// pages/orderform/orderform.js
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jthj:[],
    dqjj:[],
    ywzw:[],
    jthj1: [],//从个人中心传来的
    dqjj1: [],//从个人中心传来的
    ywzw1: [],//从个人中心传来的
    address:'',  //地址
    userInfo:'',
    cleaner:'',
    loading:false,
    payPrice:'',  //支付金额
    id:'', //从个人中心传来的未付款订单id
    outTradeNo:''  //商户平台唯一订单号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({
        id:options.id
      })
    wx.request({
      url: config.host +'api_pay/order',
      method:"POST",
      data:{id:this.data.id},
      success:res=>{
        console.log(res.data.data)
        let jthj = ''
        let dqjj = ''
        let ywzw = ''
        let num = 0
        if(res.data.data.content.dqjj){
          dqjj = res.data.data.content.dqjj
          num+=dqjj.money
        }
        if (res.data.data.content.family){
          jthj = res.data.data.content.family
          num+=jthj.money
        }
        if (res.data.data.content.ywzw){
          ywzw = res.data.data.content.ywzw
          num+=ywzw.money
        }
        this.setData({
          jthj1:jthj,
          dqjj1:dqjj,
          ywzw1:ywzw,
          address:res.data.data.address,
          cleaner:res.data.data.cleaner,
          payPrice:num,
          outTradeNo: res.data.data.out_trade_no
        })
      }
    }) 
    }
    let jthj = ''
    let dqjj = ''
    let ywzw = ''
    let num=0
    if (wx.getStorageSync("jthj")) {
      jthj = wx.getStorageSync("jthj")
      num += jthj.money
      
    }
    if (wx.getStorageSync("dqjj")) {
      dqjj = wx.getStorageSync("dqjj")
      num += dqjj.moeny
    }
    if (wx.getStorageSync("ywzw")) {
      ywzw = wx.getStorageSync("ywzw")
      num += ywzw.moeny
    }
    this.setData({
      userInfo:wx.getStorageSync("userInfor"),
      cleaner: wx.getStorageSync("cleaner").data,
      address: wx.getStorageSync("address").address,
      jthj:jthj,
      dqjj:dqjj,
      ywzw:ywzw,
      payPrice: num
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  wPay: function () {//微信支付
    console.log(this.data.outTradeNo)
    this.setData({
      loading: true
    })
    var openid = wx.getStorageSync("openid"); 
    if (this.data.outTradeNo==''){
        //code 用于获取openID的条件之一
        wx.request({
          url: config.host + 'api_pay/pay',
          method: "POST",
          data: {
            openid: openid,
            total_fee: this.data.payPrice, //金额
            cleaner: JSON.stringify(this.data.cleaner),
            address: JSON.stringify(this.data.address),
            dqjj: JSON.stringify(this.data.dqjj),
            jthj: JSON.stringify(this.data.jthj),
            ywzw: JSON.stringify(this.data.ywzw)
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: (res) => {  //后端返回的数据
            if (res.data.code == -99) {
              this.setData({
                loading: false
              })
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            } else {
              this.setData({
                loading: false
              })
              var data = res.data.data;
              console.log(data);
              console.log(data["timeStamp"]);
              wx.requestPayment({
                timeStamp: data['timeStamp'],
                nonceStr: data['nonceStr'],
                package: data['package'],
                signType: data['signType'],
                paySign: data['paySign'],
                success:  (res)=> {
                  console.log("支付成功")
                  this.goBack();
                },
                fail:  (res)=> {
                  wx.showToast({
                    title: '支付失败',
                    icon: 'none',
                    duration: 2000
                  })
                  this.goBack();
                }
              })
            }
          }
        });
    } else if (this.data.outTradeNo!=''){
      console.log("未支付完成订单")
      wx.request({
        url: config.host + 'api_pay/pay',
        method: "POST",
        data: {
          openid: openid,
          out_trade_no: this.data.outTradeNo,
          total_fee: this.data.payPrice, //金额
          cleaner: JSON.stringify(this.data.cleaner),
          address: JSON.stringify(this.data.address),
          dqjj: JSON.stringify(this.data.dqjj1),
          jthj: JSON.stringify(this.data.jthj1),
          ywzw: JSON.stringify(this.data.ywzw1)
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: (res) => {  //后端返回的数据
          if (res.data.code == -99) {
            this.setData({
              loading: false
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else {
            this.setData({
              loading: false
            })
            var data = res.data.data;
            console.log(data);
            console.log(data["timeStamp"]);
            wx.requestPayment({
              timeStamp: data['timeStamp'],
              nonceStr: data['nonceStr'],
              package: data['package'],
              signType: data['signType'],
              paySign: data['paySign'],
              success: (res) => {
                console.log("支付成功")
                this.goBack();
              },
              fail: (res) => {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none',
                  duration: 2000
                })
                this.goBack();
              }
            })
          }
        }
      });
    }
  },
  // 返回上一个并刷新
  goBack() {
    var pages = getCurrentPages();//获取页面栈
    console.log(pages)
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //调用上一个页面的onShow方法
      prePage.backList()
    }
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})